<script setup lang="ts">
defineOptions({
  name: "GAS_OPERATION_WASTE_MANAGE"
});

import { ref, computed, onMounted, onUnmounted, markRaw, getCurrentInstance, provide } from "vue";
import { queryGasWasteList, deleteGasWaste, importGasWasteData } from "@/api/modules/operation";
import commonTable from "@/views/common/components/commonTable/index.vue";
import commonSearch from "@/views/common/components/commonSearch/index.vue";
import { Edit, Delete } from "@px-ui/icons-vue";
import { PxMessage, PxMessageBox } from "px-ui";
import { useTableCloumns, useSearchOption } from "./hooks";
import { useUpdateDomHeight, useCancelUpdateDomHeight } from "@/hooks/useCommon";
import { useThrottleFn } from "@vueuse/core";
import { throttleDelay as delay } from "@/utils/constant";
import add from "./components/add.vue";
import importBottle from "./components/importBottle.vue";
import { useMessageBox } from "@/hooks/useCommon";

const searchLoading = ref<boolean>(false);
const gap = 80;
const defaultHeight = 606;
const objectModelTableRef = ref<HTMLElement | null>(null);
const commonTableRef = ref<CommonTable.TableRefType>(null);
const commonSearchRef = ref<CommonSearch.SearchFormRefType>(null);
const scrollbarHeight = ref<number>(defaultHeight);
const searchOption = ref<Array<CommonSearch.SearchOption>>(useSearchOption());
const tableCloumns = ref<Array<CommonTable.TableCloumnsType>>(useTableCloumns());
const params = ref<any>({});
// 弹框类型 add 新增   edit 编辑
const addType = ref<"add" | "edit">("add");
const bottleId = ref<string>("");
const addDialogTableVisible = ref<boolean>(false);
const importBottleVisible = ref<boolean>(false);

const operationOptions: Array<CommonTable.operationOptionItemType> = [
  {
    content: "编辑",
    auth: "GAS_OPERATION_WASTE_MANAGE_EDITBTN",
    icon: markRaw(Edit),
    click: (scope: any) => edit(scope)
  },
  {
    content: "删除",
    auth: "GAS_OPERATION_WASTE_MANAGE_DELETEBTN",
    icon: markRaw(Delete),
    type: "danger",
    click: (scope: any) => del(scope)
  }
];

let defaultParams: any = {};
let currentParams: any = {};

const updateParams = () => {
  params.value = { ...defaultParams, ...currentParams };
};

const submitSearchFormHandler = (form: any) => {
  currentParams = { ...currentParams, ...form };
  updateParams();
};

const resetSearchFormHandler = () => {
  currentParams = {};
  updateParams();
};

const addFn = () => {
  bottleId.value = "";
  addType.value = "add";
  addDialogTableVisible.value = true;
};

const { proxy }: any = getCurrentInstance();

const downloadFn = useThrottleFn(async () => {
  const url = "/operation/api/v1/gas/gasWaste/downloadTemplate";
  const params = {};
  proxy.download("post", url, params);
}, delay);

const batchImportFn = () => {
  importBottleVisible.value = true;
};

const batchExportFn = useThrottleFn(async () => {
  const url = "/operation/api/v1/gas/gasWaste/exportData";
  const params = commonTableRef.value.getParams();
  proxy.download("post", url, params);
  PxMessageBox.alert("正在导出您筛选的数据，数据处理可能需要一定时间，请耐心等待，数据准备完毕将直接下载。", "批量导出", {
    confirmButtonText: "我知道了"
  });
}, delay);

const edit = scope => {
  const {
    row: { id = "" }
  } = scope;
  bottleId.value = id;
  addType.value = "edit";
  addDialogTableVisible.value = true;
};

const del = async scope => {
  const {
    row: { wasteTypeDesc: labelName = "", id = "" }
  } = scope;
  const msg = `确定要删除废弃物 ${labelName}吗？`;
  useMessageBox("delete", msg, async () => {
    const params = { labelName, id };
    await deleteGasWaste(params);
    PxMessage.success("删除成功！");
    commonTableRef.value.queryTableData();
  });
};

const submitHandler = (flag: boolean = false) => {
  const message = flag ? "导入成功" : addType.value === "add" ? "创建成功" : "编辑成功";
  PxMessage.success(message);
  commonTableRef.value.queryTableData();
};

onMounted(async () => {
  await useUpdateDomHeight(scrollbarHeight, objectModelTableRef, gap, defaultHeight);
});

onUnmounted(() => useCancelUpdateDomHeight());
</script>

<template>
  <div v-loading="searchLoading" class="gas-waste-manage">
    <div class="gas-waste-manage-search">
      <commonSearch
        v-if="searchOption?.length"
        ref="commonSearchRef"
        v-model:loading="searchLoading"
        :row-num="4"
        :operationSpan="3"
        :option="searchOption"
        @submit="submitSearchFormHandler"
        @reset="resetSearchFormHandler"
      />
    </div>
    <div class="gas-waste-manage-btns">
      <px-button v-has="'GAS_OPERATION_WASTE_MANAGE_LEDGERMANAGE_ADDBTN'" type="primary" @click="addFn">新增</px-button>
      <px-button v-has="'GAS_OPERATION_WASTE_MANAGE_LEDGERMANAGE_DOWNLOADBTN'" type="primary" plain @click="downloadFn">模板下载</px-button>
      <px-button v-has="'GAS_OPERATION_WASTE_MANAGE_LEDGERMANAGE_IMPORTBTN'" type="primary" plain @click="batchImportFn">批量导入</px-button>
      <px-button v-has="'GAS_OPERATION_WASTE_MANAGE_LEDGERMANAGE_EXPORTBTN'" type="primary" plain @click="batchExportFn">批量导出</px-button>
    </div>
    <div ref="objectModelTableRef" class="gas-waste-manage-table">
      <commonTable
        ref="commonTableRef"
        indexNeed
        :operationOptions="operationOptions"
        :tableCloumns="tableCloumns"
        :height="scrollbarHeight"
        :axiosApi="queryGasWasteList"
        :params="params"
      />
    </div>
    <add v-if="addDialogTableVisible" :id="bottleId" v-model="addDialogTableVisible" :type="addType" @submit="submitHandler" />
    <importBottle v-if="importBottleVisible" v-model="importBottleVisible" :uploadApi="importGasWasteData" @submit="submitHandler(true)" />
  </div>
</template>

<style lang="scss" scoped>
.gas-waste-manage {
  box-sizing: border-box;
  width: 100%;
  padding: 16px;
  padding-bottom: 0;
  background-color: #fff;

  .gas-waste-manage-search {
    width: 100%;
  }

  .gas-waste-manage-btns {
    display: flex;
    align-items: center;
    margin-top: 16px;
  }

  .gas-waste-manage-table {
    margin-top: 16px;
  }
}
</style>
